import java.util.Random;

public class MarketPlaceLogic {
	/*When arriving at a planet, you need to compute the price of goods at that planet. 
	Look at the planets tech level (say it is Medieval = tech level 2). 
	You could not buy or sell Machines or Robots here. 
	The price for water would be 30 (the base price) + 3*2 (the IPL * (Planet Tech Level - MTLP)) + (variance). 
	We compute the variance by flipping a coin say heads price is increased, tails decreased. Since the water variance is 4, 
	we would pick a random number 0-4. In our example, say we threw heads and got a 2. Our final price would be:

		30 + (3*2) + (30*.02) = 30 + 6 + 6 = $42
		
		* MTLP = Minimum Tech Level to Produce this resource (You can't buy on planets below this level)
	 * MTLU = Minimum Tech Level to Use this resource (You can't sell on planets below this level)
		*/
	
	SolarSystem system;
	String event;
	Random generator;
	int[] buyArray;
	
	public MarketPlaceLogic (SolarSystem system, String event){
		this.system = system;
		this.event = event;
		this.generator = new Random();
		buyArray = new int[10];
	}
	
/*
	public int calcPrice(int BASEPRICE, int IPL, int TECHLEVEL, int MTLP, int VAR) {
		return BASEPRICE + (IPL * (TECHLEVEL - MTLP)) + VAR;
	}
		
		public int calcTraderPrice(int BASEPRICE, int IPL, int TECHLEVEL, int MTLP, int VAR) {
			return BASEPRICE + (IPL * (TECHLEVEL - MTLP)) + VAR;
		}
	}
	*/
	//put into an int array and then iterate for each jlabel
	
	public int[] buyPrices(){
		int index = 0;
		int[] array = new int[10];
		for (Goods good : Goods.values()) {
			if (good.MTLP <= system.getTechLevel()  ){
				int var = generator.nextInt(4);
				int normalPrice = good.BASEPRICE + (good.IPL * (system.getTechLevel() - good.MTLP)) + var;
			    if (good.CR==system.getResource()){
			    	array[index]= (int) (0.7* normalPrice);
			    }
			    else if (good.ER==system.getResource()){
			    	array[index]=  2 * normalPrice;
			    }
			    else{
			    	array[index]= normalPrice;
			    }
			     
			    
			    if (good.IE == event){
			    	array[index]= array[index] * 2;
			    }
			}
			else{
				array[index]= 0;
			}
			buyArray[index] = array[index];
			index++;
		}//end for
		return array; //if price is zero, don't show it
	
	}//end buyPrices
	
	public int[] sellPrices(){
		int index = 0;
		int[] array = new int[10];
		for (Goods good : Goods.values()) {
			
			if (good.MTLU <= system.getTechLevel()  ){
				int var = generator.nextInt(4);
				int normalPrice = good.BASEPRICE + (good.IPL * (system.getTechLevel() - good.MTLU)) + var;
			    if (good.CR==system.getResource()){
			    	array[index]= (int) (0.6* normalPrice);
			    }
			    else if (good.ER==system.getResource()){
			    	array[index]=  (int) (1.8 * normalPrice);
			    }
			    else{
			    	array[index]= normalPrice;
			    }
			     
			    
			    if (good.IE == event){
			    	array[index]= (int) (array[index] * 1.8);
			    }
			    
			    while(array[index]>buyArray[index]){
			    	array[index]=(int) (array[index]*0.7);
			    }
			}
			else{
				array[index]= 0;
			}
		index++;
		}//end for
		
	
		return array; //if price is zero, don't show it
	
	}//end sell
	
	/*
	 * Calculates price of particular good when trading with trade
	 * Must change System to new SolarSystem before making this call
	 * and create a new MarketPlaceLogic
	 */
	public int calcTraderPrice(Goods good) {
		int var = generator.nextInt(4);
		return good.BASEPRICE + (good.IPL * (system.getTechLevel() - good.MTLU)) + var;
	}
}
